*************************************************************************************************************
* Table B5: Effects on dealer expectations controlling for baseline values
* This table reports the effects of treatment on dealer expectations while controlling for baseline values

******************************************************************************************


use "${SurveyDataDir}/JHePOS_EL1_Dealer_data.dta",clear
tostring fps_uid, replace
drop if fps_uid == "."
merge 1:1 fps_uid using "${SurveyDataDir}/JHePOS_BL_Dealer_data.dta", keepusing(*_y0) nogen

tostring district_code, replace format("%02.0f")
tostring block_code, replace format("%03.0f")
tostring fps_code, replace format("%02.0f")
*tostring new_fps_code, replace format("%02.0f")


*Merge in Strata (need to fix this later so that we can merge on did and bid)
merge m:1 block_code using "${AdminDataDir}/blockLevel_strata.dta", keep(3) keepusing(strata)
drop _merge

qui distinct district_code block_code, j
assert `r(ndistinct)' == 132 

*merge in self reported profit endline data
preserve
use "${SurveyDataDir}/Single_FPS_Dealer_Dataset_For_Validation.dta",clear
keep fps_uid c5_profit_ration
replace c5_profit_ration = . if c5_profit_ration == -88
tempfile EL_profit_data
save `EL_profit_data'

restore

merge 1:1 fps_uid using "`EL_profit_data'", nogen

*merge in baseline variables
preserve
use "${SurveyDataDir}/JHePOS_BL_Dealer_data.dta",clear
keep fps_uid d1_leave_dealership_y0 b2_give_extra_fee_y0
tempfile baseline_data
save `baseline_data'

restore

merge 1:1 fps_uid using "`baseline_data'", nogen

preserve
use "${SurveyDataDir}/Single_FPS_Dataset_For_Validation.dta",clear
keep fps_uid c6_ration_tot_profit
destring c6_ration_tot_profit, replace
replace c6_ration_tot_profit = . if c6_ration_tot_profit == -88
tempfile baseline_profit
save `baseline_profit'

restore

merge 1:1 fps_uid using "`baseline_profit'", nogen

svyset [pw = pweight]


*Number of originally sampled FPS
scalar fps_obs = 396

*********************************************************
*BL Dealers
*********************************************************
preserve
*Keep only originally sampled dealers
keep if fps_new_yesno == 0
*3 categories - Yes,No, Maybe
recode d1_still_running_fps (1 = 3 Yes) (2 = 1 No) (3 = 2 Maybe), gen(d1_still_running_fps1)
* for baseline, yes = leave dealership, no = still running fps
recode d1_leave_dealership_y0 (0 = 1 Yes) (1 = 0 No), gen(d1_still_running_fps_y0)

drop d1_still_running_fps 

* create binaries for each income bracket 
gen d1_No = (d1_still_running_fps1 == 1) if !mi(d1_still_running_fps1)
gen d1_Maybe = (d1_still_running_fps1 == 2) if !mi(d1_still_running_fps1)
gen d1_Yes = (d1_still_running_fps1 == 3) if !mi(d1_still_running_fps1)


**
*Bribes

*Yes
gen bl_var = d1_still_running_fps_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

qui svy: mean d1_Yes if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo yesBL: xi: reg d1_Yes treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

drop bl_var bl_var_mean bl_var_mi

*Expected bribe to obtain new license
gen bl_var = b2_give_extra_fee_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

qui svy: mean b5_extra_license_today if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo new_bribeBL: xi: reg b5_extra_license_today treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

drop bl_var bl_var_mean bl_var_mi

*Expected bribe to renew license
qui svy: mean b5a_extra_license_renew if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo renew_bribeBL: xi: reg b5a_extra_license_renew treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

*Dealer Self-Reported Profits
gen bl_var = c6_ration_tot_profit
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

qui svy: mean c5_profit_ration if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo profitBL: xi: reg c5_profit_ration treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

drop bl_var bl_var_mean bl_var_mi

restore

*********************************************************
*All dealers
*********************************************************

recode d1_still_running_fps (1 = 3 Yes) (2 = 1 No) (3 = 2 Maybe), gen(d1_still_running_fps1)
recode d1_leave_dealership_y0 (0 = 1 Yes) (1 = 0 No), gen(d1_still_running_fps_y0)

drop d1_still_running_fps 

* create binaries for each income bracket 
gen d1_No = (d1_still_running_fps1 == 1) if !mi(d1_still_running_fps1)
gen d1_Maybe = (d1_still_running_fps1 == 2) if !mi(d1_still_running_fps1)
gen d1_Yes = (d1_still_running_fps1 == 3) if !mi(d1_still_running_fps1)


**
*Bribes
gen bl_var = d1_still_running_fps_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

*Yes
qui svy: mean d1_Yes if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo yes: xi: reg d1_Yes treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

drop bl_var bl_var_mean bl_var_mi

*Expected bribe to obtain new license
gen bl_var = b2_give_extra_fee_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

qui svy: mean b5_extra_license_today if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo new_bribe: xi: reg b5_extra_license_today treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code) 
estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

drop bl_var bl_var_mean bl_var_mi

*Expected bribe to renew license
qui svy: mean b5a_extra_license_renew if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo renew_bribe: xi: reg b5a_extra_license_renew treatment i.strata [pw = pweight], cluster(block_code) 

estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

*Self-Reported Dealer Profit
gen bl_var = c6_ration_tot_profit
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

qui svy: mean c5_profit_ration if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo profit: xi: reg c5_profit_ration treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code) 

estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

drop bl_var bl_var_mean bl_var_mi

*indicate("Strata FE = *strata*") rename(1.treatment treatment)  drop(_cons ) coeflabels(treatment "Treatment")

#delimit ;
						 
esttab yes yesBL new_bribe new_bribeBL renew_bribe renew_bribeBL profit profitBL using "${OutputDir}/TableB_5.tex", 
	label b(%12.2gc) se(%12.2gc) booktabs replace nocons width(\hsize)
	rename(1.treatment treatment)  drop(_cons bl_var bl_var_mi _Istrata*) coeflabels(treatment "Treatment")
	stats(r2_a control_mean N percent_obs esample, 
		labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample" "Sample") fmt(%12.2g %12.2gc %15.0fc 0 ))
	star(* .10 ** .05 *** .01)
	mgroups("\specialCellCenter{Intends to continue \\ running FPS?}" "\specialCellCenter{Expected bribes \\ to obtain license?}" "\specialCellCenter{Expected bribes \\ to renew license?}" "\specialCellCenter{Profit (self-reported)}",
		pattern(1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(})  
		span erepeat(\cmidrule(lr){@span}))		
	mlabel(none)
	substitute("main                & " ""
						 "Standard errors in parentheses" ""
						 "\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "
						 " &           .         &"
						 "&                    &"
						 "\multicolumn{7}{l}{\footnotesize  }\\" ""
						 "\multicolumn{7}{l}{\footnotesize }\\" "");

	
#delimit cr
eststo clear 
